﻿using SqlScriptVault.Commands;
using SqlScriptVault.Model;
using SqlScriptVault.Properties;
using SqlScriptVault.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SqlScriptVault.ViewModel
{
    class ServerViewModel
    {
        private readonly IMessageDisplayService _messageDisplayService;

        public ServerViewModel()
            : this(new MessageDisplayService())
        {
        }

        public ServerViewModel(IMessageDisplayService messageDisplayService)
        {
            _messageDisplayService = messageDisplayService;

            Server = new Server();

            ServerTypes = new List<string>(new Settings().ServerTypes.Split('|'));
        }

        public Server Server { get; private set; }

        public List<string> ServerTypes { get; private set; }

        public RelayCommand CommitCommand
        {
            get
            {
                return new RelayCommand(param => Commit());
            }
        }

        public event Action RequestClose;

        public void Commit()
        {
            if (string.IsNullOrEmpty(Server.ServerName))
            {
                _messageDisplayService.ShowErrorMessage("Server name can not be empty.");
                return;
            }

            if (string.IsNullOrEmpty(Server.DatabaseName))
            {
                _messageDisplayService.ShowErrorMessage("Database name can not be empty.");
                return;
            }

            if (string.IsNullOrEmpty(Server.ServerType))
            {
                _messageDisplayService.ShowErrorMessage("Server type can not be empty.");
                return;
            }

            RequestClose();
        }
    }
}
